#bilibili用户系统


from PyQt5.QtWidgets import *
from PyQt5.uic import loadUiType
import project
import encol_ui
import reset_ui
import contract_ui
import privacy_ui
from index import *
import hashlib
import tu_rc
import sys

# UI--Logic分离
enter, _ = loadUiType('enter.ui')

class EnterApp(QMainWindow,enter):

    #定义构造方法
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.handle_buttons()
        self.show()

    def handle_buttons(self):
        self.enter_agree_Button.clicked.connect(self.handle_login)
        self.enter_enrol_Button.clicked.connect(self.encol)
        self.enter_change_password_Button.clicked.connect(self.reset)
        self.enter_xieyi_Button.clicked.connect(self.contract)
        self.enter_zhengce_Button.clicked.connect(self.privacy)


    def encol(self):
        self.encol_app = encol_ui.EncolApp()
        self.close()
        self.encol_app.show()

    def reset(self):
        self.reset_app = reset_ui.ResetApp()
        self.close()
        self.reset_app.show()

    def contract(self):
        self.contract_app = contract_ui.ContractApp()
        self.close()
        self.contract_app.show()

    def privacy(self):
        self.privacy_app = privacy_ui.PrivacyApp()
        self.close()
        self.privacy_app.show()


    # MD5加密处理
    def md5(self, arg):
        hash = hashlib.md5(bytes("禾木AI", encoding="utf-8"))
        hash.update(bytes(arg, encoding='utf-8'))
        return hash.hexdigest()

    def handle_login(self):
        conn = get_conn()
        cur = conn.cursor()
        sql = "select * from users where user_num=%s and user_pwd=%s"
        user_num = self.enter_accounts_Edit.text()
        user_pwd = self.enter_password_Edit.text()
        pwd = self.md5(user_pwd)
        cur.execute(sql, (user_num, pwd))
        data = cur.fetchone()

        if data:
            self.main_app = project.MainApp()
            self.close()
            self.main_app.show()
        else:
            self.statusBar().showMessage("账号或密码错误，登陆失败！")
            self.enter_accounts_Edit.setText('')
            self.enter_password_Edit.setText('')



def main():
    app = QApplication([])
    window = EnterApp()
    window.show()
    app.exec_()


if __name__ == '__main__':
    main()


